import request from "./request";
import { API_URL } from "../constants";
import type {
  IProgram,
  IProgramStatus,
  IResponse,
  IResponseList,
} from "../types";

export function getProgramList(
  data: { keyword?: string; modelNo?: string },
  token: string,
) {
  const formData = new FormData();
  for (const key in data) {
    const _ = key as keyof typeof data;
    if (data[_]) {
      formData.append(key, data[_]);
    }
  }
  return request<IResponseList<{ rows: IProgram[] }>>(
    `${API_URL}/business/program/list`,
    {
      method: "post",
      data: formData,
      headers: { Authorization: token },
    },
  );
}

export function getProgramInfo(id: number, token: string) {
  return request<IResponse<{ data: IProgram }>>(
    `${API_URL}/business/program/${id}`,
    {
      headers: { Authorization: token },
    },
  );
}

export function getProgramStatus(id: number, token: string) {
  return request<IResponse<{ data: IProgramStatus[] }>>(
    `${API_URL}/business/programDetail/${id}`,
    {
      headers: { Authorization: token },
    },
  );
}

export function updateProgramStatus(
  id: number,
  data: IProgramStatus[],
  token: string,
) {
  return request<IResponse>(`${API_URL}/business/programDetail/${id}`, {
    method: "put",
    data,
    headers: { Authorization: token },
  });
}

export function updateProgram(
  data: Partial<Omit<IProgram, "programId">> & { programId: number },
  token: string,
) {
  return request<IResponse>(`${API_URL}/business/program`, {
    method: "put",
    data,
    headers: { Authorization: token },
  });
}

export function createProgram(programName: string, token: string) {
  return request<IResponse>(`${API_URL}/business/program`, {
    method: "post",
    data: { programName },
    headers: { Authorization: token },
  });
}

export function deleteProgram(id: number, token: string) {
  return request<IResponse>(`${API_URL}/business/program/${id}`, {
    method: "delete",
    headers: { Authorization: token },
  });
}

export function copyProgram(programId: number, token: string) {
  return request<IResponse>(`${API_URL}/business/program/copy/${programId}`, {
    headers: { Authorization: token },
  });
}
